/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <p>Project: javaseapp - Map1
 * <p>Powered by webrx On 2021-08-10 08:46:28
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Map1 {
    public static void main(String[] args) {
        //List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        //Map<String, Object> map = new HashMap<>();
        //var set = new HashSet<String>();
        Map<String, String> map = new HashMap<>();
        map.put("1", "java");
        System.out.println(map.size());
        map.clear();//清空map
        System.out.println(map.isEmpty());//true

        map.put("k1", "java");
        map.put("k2", "javascript");
        map.put("k2", "myjava");
        map.put("k3", "php");
        map.put("k4", "mysql");
        map.put("k5", "redis");
        System.out.println(map.containsKey("k6"));//false
        //map.containsKey()
        System.out.println(map.containsValue("html"));//false
        System.out.println(map.containsValue("redis"));//true
        //map.containsValue()
        System.out.println(map.size());//5

        System.out.println(map.get("k2"));//myjava
        map.put("k9", "python");
        map.put("k6", "typescript");
        //读取map中指定的key的值，如果有就返回，如果没有就使用第二个参数为默认值返回
        Object lang = map.getOrDefault("k6", "c");
        System.out.println(lang);

        map.remove("k6");
        map.remove("k9");
        map.remove("k9", "python");


        Collection<String> col = map.values();
        System.out.println(col);

        for (Object obj : col) {
            System.out.println(obj);
        }

        Set<String> keys = map.keySet();
        System.out.println(keys);
        for (String k : keys) {
            System.out.printf("%s = %s %n", k, map.get(k));
        }
        System.out.println("----------------------------------");
        // 遍历方法二
        map.forEach((k, v) -> System.out.printf("%s = %s %n", k, v));


    }
}
